package cq.zhcqk.activity;

import android.support.v7.widget.GridLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dou361.dialogui.DialogUIUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
//import com.mrw.wzmrecyclerview.Divider.BaseItemDecoration;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemClickListener;
//import com.mrw.wzmrecyclerview.HeaderAndFooter.OnItemLongClickListener;
//import com.mrw.wzmrecyclerview.PullToLoad.OnLoadListener;
//import com.mrw.wzmrecyclerview.PullToLoad.PullToLoadRecyclerView;
//import com.mrw.wzmrecyclerview.PullToRefresh.OnRefreshListener;
//import com.mrw.wzmrecyclerview.SimpleAdapter.SimpleAdapter;
//import com.mrw.wzmrecyclerview.SimpleAdapter.ViewHolder;
import com.jimi_wu.ptlrecyclerview.Divider.BaseItemDecoration;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemClickListener;
import com.jimi_wu.ptlrecyclerview.HeaderAndFooter.OnItemLongClickListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.OnLoadListener;
import com.jimi_wu.ptlrecyclerview.PullToLoad.PullToLoadRecyclerView;
import com.jimi_wu.ptlrecyclerview.PullToRefresh.OnRefreshListener;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.SimpleAdapter;
import com.jimi_wu.ptlrecyclerview.SimpleAdapter.ViewHolder;

import org.apache.http.Header;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Map;

import cq.zhcqk.R;
import cq.zhcqk.base.BaseActivity;
import cq.zhcqk.util.CheckLogin;
import cq.zhcqk.util.Common;
import cq.zhcqk.util.GsonUtil;

/**
 * Created by zhangle on 2017/11/30.
 */
public class DicListActivity extends BaseActivity implements View.OnClickListener{
    private PullToLoadRecyclerView rcv;
    private ArrayList<Map<String, Object>> list;
    private int page = 1;
    private String offset = "15";
    private String id = "";

    private ImageView header_back;
    private TextView header_text;
    private EditText pinglun_text;
    private ImageView shouchang;
    private ImageView pinglun_commit;
    @Override
    protected void initView() {
        setContentView(R.layout.activity_list_dic);
        initRecyclerView();
        id = getIntent().getStringExtra("cid");
        System.out.println("id:"+id);
        pinglun_text = (EditText) findViewById(R.id.pinglun_text);
        shouchang = (ImageView) findViewById(R.id.shouchang);
        pinglun_commit = (ImageView) findViewById(R.id.pinglun_commit);
        header_back = (ImageView) findViewById(R.id.header_back);
        header_text = (TextView) findViewById(R.id.header_text);

        shouchang.setOnClickListener(this);
        pinglun_commit.setOnClickListener(this);
        header_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        header_text.setText("评论");
        getDic(id,page);
    }

    private void initRecyclerView(){
        rcv = (PullToLoadRecyclerView)findViewById(R.id.rc_detail);
        rcv.setRefreshEnable(false);
        rcv.setLayoutManager(new GridLayoutManager(context,1));
//        设置适配器，封装后的适配器只需要实现一个函数
        list = new ArrayList<>();
        rcv.setAdapter(new SimpleAdapter<Map<String, Object>>(context, list, R.layout.item_pinglun) {
            @Override
            protected void onBindViewHolder(ViewHolder holder, Map<String, Object> data) {
                holder.<TextView>getView(R.id.p_time).setText(""+data.get("time"));
                holder.<TextView>getView(R.id.p_content).setText(""+data.get("discusscontent"));
            }
        });

//        设置刷新监听
        rcv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onStartRefreshing() {
            }
        });
//        设置加载监听
        rcv.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onStartLoading(int skip) {
            }
        });
//        设置分割线
        rcv.addItemDecoration(new BaseItemDecoration(context,R.color.gray_bg));
        rcv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void OnItemClick(int position) {
            }
        });
        rcv.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(int position) {
                return true;
            }
        });
    }

    private void getDic(String cid,int limit){
        RequestParams params = new RequestParams();
        params.add("dataid",cid);
        params.add("limit",offset);
        params.add("offset",limit+"");
        httpClient.post(Common.URL+Common.NEWS_DCTLIST,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Map<String, Object> map = GsonUtil.GsonToMaps(response+"");
                ArrayList<Map<String, Object>> listNew = null;
                listNew = (ArrayList<Map<String, Object>>)((Map<String, Object>)map.get("data")).get("rows");
                if(listNew == null || listNew.size()==0){
                    Toast.makeText(context,"没有更多",Toast.LENGTH_SHORT).show();
                }else{
                    list.addAll(listNew);
                }
                rcv.completeRefresh();
                rcv.completeLoad(1);
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure:"+errorResponse+"");
//                Toast.makeText(context,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                System.out.println("onFailure:"+responseString+"");
//                Toast.makeText(context,"登录失败，请检查用户名或密码",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void pinglun(String discusscontent){
        RequestParams params = new RequestParams();
        params.add("dataid",id);
        params.add("discusscontent",discusscontent);
        httpClient.post(Common.URL+Common.NEWS_DIC,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Toast.makeText(context,"发布评论成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
//                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    private void shouChang(){
        RequestParams params = new RequestParams();
        params.add("cid",id);
        httpClient.post(Common.URL+Common.NEWS_DCT,params,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers,
                                  JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                System.out.println("onSuccess："+response);
                Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers,
                                  Throwable throwable, JSONObject errorResponse) {
                super.onFailure(statusCode, headers, throwable,
                        errorResponse);
                System.out.println("onFailure："+errorResponse);
                CheckLogin.checkLogin(context,errorResponse+"");
//                Toast.makeText(context,"发布评论失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                Toast.makeText(context,"收藏失败，请稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFinish() {
                super.onFinish();
                DialogUIUtils.dismiss(buildBean);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.header_back:
                finish();
                break;
            case R.id.pinglun_commit:
                String discusscontent = pinglun_text.getText()+"";
                if("".equals(discusscontent) || discusscontent == null){
                    Toast.makeText(context,"请输入评论内容",Toast.LENGTH_SHORT).show();
                    return;
                }
                buildBean.show();
                pinglun(discusscontent);
                break;
            case R.id.shouchang:
                buildBean.show();
                shouChang();
                break;
        }
    }
}
